package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * COMDCB类
 * 实现串口通信参数的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class COMDCB implements AxdrType {

    private byte[] dataCode = null;
    private AxdrEnum baut = null;
    private AxdrEnum parity = null;
    private AxdrEnum datab = null;
    private AxdrEnum stopb = null;
    private AxdrEnum flow = null;

    /**
     * 默认构造函数
     */
    public COMDCB() {
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置所有参数
     * 
     * @param baut   波特率
     * @param parity 校验位
     * @param datab  数据位
     * @param stopb  停止位
     * @param flow   流控制
     */
    public void setAll(AxdrEnum baut, AxdrEnum parity, AxdrEnum datab, AxdrEnum stopb, AxdrEnum flow) {
        this.baut = baut;
        this.parity = parity;
        this.datab = datab;
        this.stopb = stopb;
        this.flow = flow;
    }

    /**
     * 设置波特率
     * 
     * @param baut 波特率
     */
    public void setBaut(AxdrEnum baut) {
        this.baut = baut;
    }

    /**
     * 设置校验位
     * 
     * @param parity 校验位
     */
    public void setParity(AxdrEnum parity) {
        this.parity = parity;
    }

    /**
     * 设置数据位
     * 
     * @param datab 数据位
     */
    public void setDatab(AxdrEnum datab) {
        this.datab = datab;
    }

    /**
     * 设置停止位
     * 
     * @param stopb 停止位
     */
    public void setStopb(AxdrEnum stopb) {
        this.stopb = stopb;
    }

    /**
     * 设置流控制
     * 
     * @param flow 流控制
     */
    public void setFlow(AxdrEnum flow) {
        this.flow = flow;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        baut = new AxdrEnum();
        baut.setConst();
        codeLength += baut.decode(input);

        parity = new AxdrEnum();
        parity.setConst();
        codeLength += parity.decode(input);

        datab = new AxdrEnum();
        datab.setConst();
        codeLength += datab.decode(input);

        stopb = new AxdrEnum();
        stopb.setConst();
        codeLength += stopb.decode(input);

        flow = new AxdrEnum();
        flow.setConst();
        codeLength += flow.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (dataCode != null) {
            codeLength = dataCode.length;
            for (int i = dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (baut != null && parity != null && datab != null && stopb != null && flow != null) {
                codeLength = 0;
                codeLength += flow.encode(output);
                codeLength += stopb.encode(output);
                codeLength += datab.encode(output);
                codeLength += parity.encode(output);
                codeLength += baut.encode(output);
            } else {
                throw new RuntimeException("flow || stopb || datab || parity || baut is null");
            }
        }
        return codeLength;
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (baut != null && parity != null && datab != null && stopb != null && flow != null) {
            return "sequence: {" + "baut: " + baut + ", parity: " + parity + ", datab: " + datab + ", stopb: " + stopb
                    + ", flow: " + flow + "}";
        } else {
            return "flow or stopb or datab or parity or baut is null";
        }
    }

    // Getter methods
    public AxdrEnum getBaut() {
        return baut;
    }

    public AxdrEnum getParity() {
        return parity;
    }

    public AxdrEnum getDatab() {
        return datab;
    }

    public AxdrEnum getStopb() {
        return stopb;
    }

    public AxdrEnum getFlow() {
        return flow;
    }
}